package com.wuge.auth.controller;

import com.wuge.auth.feign.MemberFeign;
import com.wuge.auth.vo.UserRegisterVo;
import com.wuge.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@Controller
public class RegisterController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private MemberFeign memberFeign;
    @PostMapping ("/register")
 public String register(@Valid UserRegisterVo userRegisterVo, BindingResult bindingResult, RedirectAttributes redirectAttributes){
        if (bindingResult.hasErrors()) {
            HashMap<String,String> errors=new HashMap<>();
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                errors.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            //重定向传递数据放到session中
            redirectAttributes.addFlashAttribute("errors",errors);
//            保证注册幂等性如果采用转发刷新带着原来的值
            return "redirect:http://auth.gulimall.com/reg.html";
        }
//        判断验证码是否正确
        String s = stringRedisTemplate.opsForValue().get("phone_code" + userRegisterVo.getPhone());
        if (!StringUtils.isEmpty(s)){
            HashMap<String,String> errors=new HashMap<>();

            String[] s1 = s.split("_");
            if (!s1[0].equals(userRegisterVo.getCode())) {
                errors.put("code","验证码输入有误");
                redirectAttributes.addFlashAttribute("errors",errors);
//            保证注册幂等性如果采用转发刷新带着原来的值
                return "redirect:http://auth.gulimall.com/reg.html";
            }

        }else{
            HashMap<String,String> errors=new HashMap<>();
            errors.put("code","验证码输入有误");
            redirectAttributes.addFlashAttribute("errors",errors);
//            保证注册幂等性如果采用转发刷新带着原来的值
            return "redirect:http://auth.gulimall.com/reg.html";
        }
//        验证验证码正确之后删除redis的验证码
        stringRedisTemplate.delete("phone_code" + userRegisterVo.getPhone());
        R r = memberFeign.register(userRegisterVo);
        if (r.getCode()==0){
            return "redirect:http://auth.gulimall.com/login.html";
        }
        else{
         if (r.getCode()==10003){
             HashMap<String,String> errors=new HashMap<>();
             errors.put("phone","该号码已被注册");
             redirectAttributes.addFlashAttribute("errors",errors);
//            保证注册幂等性如果采用转发刷新带着原来的值
             return "redirect:http://auth.gulimall.com/reg.html";
         }
         else {

                 HashMap<String,String> errors=new HashMap<>();
                 errors.put("userName","该号码已被注册");
                 redirectAttributes.addFlashAttribute("errors",errors);
//            保证注册幂等性如果采用转发刷新带着原来的值
                 return "redirect:http://auth.gulimall.com/reg.html";

         }
        }

 }
}
